﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TrayCheckControl
{
    public delegate void SaveSetHandler(EnvironmentSet envSet);
    public partial class WindowSet : Form
    {
        private static int instanceCount = 0;
        public static int InstanceCount
        {
            get => WindowSet.instanceCount;
            private set
            {
                WindowSet.instanceCount = value;
            }
        }
        public event SaveSetHandler SaveSet;
        private EnvironmentSet envSet = new EnvironmentSet();
        public EnvironmentSet EnvSet
        {
            get => this.envSet;
            set
                {
                this.envSet.Copy(value);
            }
        }
        public WindowSet()
        {
            ++WindowSet.InstanceCount;
            InitializeComponent();
            this.InitBindings();
        }

        private void InitBindings()
        {
            this.textBoxIp.DataBindings.Add(new Binding("Text", this.envSet, "ServerSet.Ip", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxPort.DataBindings.Add(new Binding("Text", this.envSet, "ServerSet.Port", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxIdType.DataBindings.Add(new Binding("Text", this.envSet, "CarrierSet.IdType", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxStation.DataBindings.Add(new Binding("Text", this.envSet, "CarrierSet.Station", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxSeperator.DataBindings.Add(new Binding("Text", this.envSet, "CarrierSet.Seperator", false, DataSourceUpdateMode.OnPropertyChanged));
            //this.textBoxLock.DataBindings.Add(new Binding("Text", this.envSet, "CarrierSet.CommandLock", false, DataSourceUpdateMode.OnPropertyChanged));
            this.checkBoxBypass.DataBindings.Add(new Binding("Checked", this.envSet, "Bypass", false, DataSourceUpdateMode.OnPropertyChanged));

            this.textBoxIp.DataBindings.Add(new Binding("Enabled", this.envSet, "Enable", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxPort.DataBindings.Add(new Binding("Enabled", this.envSet, "Enable", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxIdType.DataBindings.Add(new Binding("Enabled", this.envSet, "Enable", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxStation.DataBindings.Add(new Binding("Enabled", this.envSet, "Enable", false, DataSourceUpdateMode.OnPropertyChanged));
            //this.textBoxLock.DataBindings.Add(new Binding("Enabled", this.envSet, "Enable", false, DataSourceUpdateMode.OnPropertyChanged));
            this.textBoxSeperator.DataBindings.Add(new Binding("Enabled", this.envSet, "Enable", false, DataSourceUpdateMode.OnPropertyChanged));
        }

        private void WindowClosing(object sender, FormClosingEventArgs e)
        {
            --WindowSet.InstanceCount;
        }

        private void Button_Click(object sender, EventArgs e)
        {
            if(sender.Equals(this.buttonOk))
            {
                this.Save();
                this.Close();
            }
            if(sender.Equals(this.buttonCancel))
            {
                this.Close();
            }
        }
        private void Save()
        {
            if(null != this.SaveSet)
            {
                this.SaveSet(this.envSet);
            }
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            System.Net.IPAddress ipAddr;
            int port;
            if(sender.Equals(this.textBoxIp))
            {
                if(System.Net.IPAddress.TryParse(this.textBoxIp.Text, out ipAddr))
                {
                    this.textBoxIp.BackColor = Color.White;
                    this.textBoxIp.ForeColor = Color.Black;
                }
                else
                {
                    this.textBoxIp.BackColor = Color.Red;
                    this.textBoxIp.ForeColor = Color.White;                    
                }
            }
            if(sender.Equals(this.textBoxPort))
            {
                if (int.TryParse(this.textBoxPort.Text, out port))
                {
                    this.textBoxPort.BackColor = Color.White;
                    this.textBoxPort.ForeColor = Color.Black;
                }
                else
                {
                    this.textBoxPort.BackColor = Color.Red;
                    this.textBoxPort.ForeColor = Color.White;
                }
            }
        }
    }
}
